<?php
/** .-------------------------------------------------------------------
* |    Author: 熊伟洋 <chelious@foxmail.com>
* |    WeChat: hello_McGrady
* |    	   QQ: 434493420
* |     Motto: Hungry & Humble
* |---------------------------------------------------------------------
* |    Copyright (c) 2012-2020, www.chelious.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;

/**
 * Class Tag
 * @package app\admin\controller
 * 标签管理控制器
 */

class Tag extends Common{
    private $model;

    public function __construct()
    {
        $this->model = new \system\model\Tag();
        parent::__construct();
    }


    //列表页
    public function index(){
        $data = Db::table('tag')->paginate(10);
        $page = $data->links();
        View::with(['data' => $data , 'page' => $page]);

        return view();
    }

    //添加标签
    public function add(){

        if(IS_POST){
//            p(Q('post.tname'));
            if($this->model->add()){
                //添加成功
                message('添加成功',u('admin.tag.index'),'success');

            }else{
                //添加失败
                message($this->model->getError(),'back','error');
            }

        }

        return view();
    }

    //编辑标签
    public function edit(){
        $tid = Q('get.tid');
//        p($tid);
        $oldData = Db::table('tag')->where('tid',$tid)->first();
//        p($oldData);
        View::with('oldData',$oldData);
        if(IS_POST){
            if($this->model->edit()){
                message('修改成功',u('admin.tag.index'),'success');
            }else{
                message($this->model->getError(),'back','error');
            }
        }

        return view();
    }

    //删除标签
    public function del(){
        $tid = Q('get.tid');
//        p($tid);
        $rows = $this->model->where('tid',$tid)->delete();      //为影响的行数    删除操作
        if($rows){
            //删除成功
            message('删除成功',u('admin.tag.index'),'success');

        }else{
            //删除失败
            message('删除失败',u('admin.tag.index'),'error');
        }

    }

}
